// pages/index/login.js


const rsa = require("../../utils/rsa");
const app=getApp();
const rsaPublicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdSynHW2t2yPS7XpDS8QVC4KEej8JcWQSla2jxB1YmT3dWH5oP6x6CxoMLW0ZoDApikli4M+QKQ+uT30mew/Yn/GKKjdpxjknUCJ45jqS+PWdgzlJaQgAjnjuINhe8AIrZcSSxsHDc164dZmc/QH+LazqPc5LlZ6VvyzJp9FdCBwIDAQAB";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account:null,
    password: null,
    hidePassword: true
  },

  encryptData: function (data) {
    const cryptoKey="1234567890123456";
    //设置密钥
    const SELECT_KEY = CryptoJS.enc.Utf8.parse(cryptoKey);
    const dataHex = CryptoJS.enc.Utf8.parse(data);
    const encrypted = CryptoJS.AES.encrypt(dataHex, SELECT_KEY, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString((CryptoJS.format).Base64);
  },

  loginHandle: function (e) {
    //const encryptPassword =rsa.encryptByPublicKey( this.data.password,rsaPublicKey);
    const  encryptPassword =  app.encryptData(this.data.password);
    const payload={"username": this.data.account,"password": encryptPassword,"openId": wx.getStorageSync('openId')} ;
    app.request.post('/app/user/login', payload, (res) => {
      console.log('登陆返回:', res);
      if(res.code ===0){
        wx.setStorageSync('userInfo',  res.data)
        wx.navigateBack() ;
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        });
      }     
    },(err)=>{
      wx.showToast({
        title: '网络连接失败',
        icon: 'none',
        duration: 2000
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})